﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Mvc;
using yiCommerce.Core;
using yiCommerce.Core.Domain.Products;
using yiCommerce.Core.Domain.ShoppingCarts;
using yiCommerce.Core.Helper;
using yiCommerce.Service.Loggings;
using yiCommerce.Service.Products;
using yiCommerce.Service.ShoppingCarts;

namespace yiCommerce.Controllers.api
{
    public class AddToShoppingCartResult
    {
        public string Status { get; set; }
        public string Message { get; set; }
    }
    public class ShoppingCartApiController : ApiController
    {
        private IProductService _productService;
        private IShoppingCartService _shoppingCartService;
        private IWorkContext _workContext;
        private IActivityService _activityService;


        public ShoppingCartApiController(IProductService productService, IShoppingCartService shoppingCartService, IWorkContext workContext, IActivityService activityService)
        {
            _productService = productService;
            _shoppingCartService = shoppingCartService;
            _workContext = workContext;
            _activityService = activityService;
        }

        public AddToShoppingCartResult AddProductVariantToCart(AddToCartProductModel addToCartProductModel)
        {
            var result = new AddToShoppingCartResult();

            var productVariant = _productService.GetProductVariantById(addToCartProductModel.ProductVariantId);
            if (productVariant == null)
            {
                result.Status = "1";
                result.Message = "product variant not exist";
                return result;
            }

            //quantity
            var quantity = addToCartProductModel.Quantity;

            var attributes = BuildProductVariantAttributeValue(addToCartProductModel.ProductVariantAttributeValueIds);

            var addToCartWarnings = new List<string>();

            addToCartWarnings.AddRange(_shoppingCartService.AddToCart(_workContext.CurrentCustomer.Id,
                                                                      productVariant, attributes, quantity));

            if (addToCartWarnings.Count > 0)
            {

                result.Status = "1";
                result.Message = string.Join(",",addToCartWarnings);
                return result;
            }

            //activity log
            _activityService.InsertActivity("PublicStore.AddToShoppingCart", JsonHelper.ToJson(addToCartProductModel),
                                            _workContext.CurrentCustomer);

            result.Status = "0";
            return result;

        }


        private string BuildProductVariantAttributeValue(IList<int> productVariantAttributeValueIds)
        {
            var xmls = new List<ProductVariantAttributeValueXml>();

            foreach (var productVariantAttributeValueId in productVariantAttributeValueIds)
            {
                var pvAv =
                    _productService.GetProductVariantAttributeValueByProductVariantAttributeValueId(
                        productVariantAttributeValueId);

                var xml = new ProductVariantAttributeValueXml()
                    {
                        AttributeValueId = productVariantAttributeValueId,
                        AttributeName = pvAv.ProductVariantAttribute.ProductAttribute.Name,
                        AttributeValue = pvAv.Name
                    };
                xmls.Add(xml);

            }
            return JsonHelper.ToJson(xmls);
        }
    }
}
